﻿using System.Collections.Generic;
using System.Threading.Tasks;
using XCode;
using Zdap.Common;

namespace Zdap.XCode.Service
{
    /// <summary>
    /// 查询数据
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TDto"></typeparam>
    public abstract class SearchServiceBase<TEntity, TDto> : ISearchServiceBase<TDto> where TDto : class where TEntity : Entity<TEntity>, new()
    {
        /// <summary>
        /// 是否启用 允许无参数，查询整个表的数据,默认：false
        /// </summary>
        public virtual bool EnableAllowNoParameter { get; protected set; } = false;

        #region 数据获取

        /// <summary>
        /// 分页查询 不返回 列头
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public virtual async Task<PageResponseContext<TDto>> Search(Pager p)
        {
            if (!EnableAllowNoParameter)// 没有启用全表数据查询，移除掉 允许全表查询参数
                p.Params.Remove(CommonService.AllowNoParameter);
            else//启用无条件查询 全表数据
            {
                p.Params.TryAdd(CommonService.AllowNoParameter, EnableAllowNoParameter);
            }
            return await CommonService.SearchAsync<TEntity, TDto>(p);

            //return await SearchAsync<TEntity, TDto>(p);
        }

        ///// <summary>
        ///// 分页查询 并且 返回 列头
        ///// </summary>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //public virtual async Task<PageResponseContext2<TDto>> SearchAsync(Pager p)
        //{
        //    return await SearchAsync<TEntity, TDto>(p);
        //}

        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <typeparam name="TModel"></typeparam>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //public virtual async Task<PageResponseContext2<TModel>> SearchAsync<TModel>(Pager p) where TModel : class
        //{
        //    return await SearchAsync<TEntity, TModel>(p);
        //}

        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <typeparam name="TModel"></typeparam>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //protected virtual async Task<PageResponseContext2<TModel>> SearchAsync<TEntity, TModel>(Pager p) where TModel : class where TEntity : Entity<TEntity>, new()
        //{
        //    if (!EnableAllowNoParameter)// 没有启用全表数据查询，移除掉 允许全表查询参数
        //        p.Params.Remove(CommonService.AllowNoParameter);
        //    else//启用无条件查询 全表数据
        //    {
        //        p.Params.TryAdd(CommonService.AllowNoParameter, EnableAllowNoParameter);
        //    }
        //    return await CommonService.SearchAsync<TEntity, TModel>(p);
        //}

        /// <summary>
        /// 查询所有数据 不分页
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public virtual async Task<ListResponseContext<TDto>> SearchAll(RequestParameter p)
        {
            var result = new ListResponseContext<TDto>();
            //if (p.Params.Count == 0)
            //    return result.SetSuccess(null);

            if (!EnableAllowNoParameter)// 没有启用全表数据查询，移除掉 允许全表查询参数
            {
                p.Params.Remove(CommonService.AllowNoParameter);
                if (p.Params.Count == 0)
                {
                    return result.SetError("请传入有效的参数。");
                }
            }
            else//启用无条件查询 全表数据
            {
                p.Params.TryAdd(CommonService.AllowNoParameter, EnableAllowNoParameter);
            }

            var datas = await CommonService.SearchAll<TEntity, TDto>(p);

            return result.SetSuccess(datas);
        }

        ///// <summary>
        ///// 查询所有数据 不分页
        ///// </summary>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //public virtual async Task<ListResponseContext<TDto>> SearchAll(IDictionary<string, object> param)
        //{
        //    var result = new ListResponseContext<TDto>();
        //    if (param.Count == 0)
        //        return result.SetSuccess(null);

        //    if (!EnableAllowNoParameter)
        //        param.Remove(CommonService.AllowNoParameter);

        //    var datas = await CommonService.SearchAll<TEntity, TDto>(param);

        //    return result.SetSuccess(datas);
        //}

        ///// <summary>
        ///// 构建查询条件
        ///// </summary>
        ///// <param name="param"></param>
        ///// <returns></returns>
        //protected WhereExpression BuildWhereExpression(IDictionary<string, object> param)
        //{
        //    if (!EnableAllowNoParameter)// 没有启用全表数据查询，移除掉 允许全表查询参数
        //        param.Remove(CommonService.AllowNoParameter);
        //    else//启用无条件查询 全表数据
        //    {
        //        param.TryAdd(CommonService.AllowNoParameter, EnableAllowNoParameter);
        //    }

        //    return BuildWhereExpression<TEntity>(param);
        //}

        ///// <summary>
        ///// 构建查询条件
        ///// </summary>
        ///// <param name="param"></param>
        ///// <returns></returns>
        //protected static WhereExpression BuildWhereExpression<TEntity>(IDictionary<string, object> param) where TEntity : Entity<TEntity>, new()
        //{
        //    return CommonService.BuildWhereExpression<TEntity>(param);
        //}

        ///// <summary>
        ///// 查找所有数据
        ///// </summary>
        ///// <returns></returns>
        //public virtual async Task<ListResponseContext<TDto>> FindAllAsync(IDictionary<string, object> param)
        //{
        //    if (!EnableAllowNoParameter)// 没有启用全表数据查询，移除掉 允许全表查询参数
        //        param.Remove(CommonService.AllowNoParameter);
        //    else//启用无条件查询 全表数据
        //    {
        //        param.TryAdd(CommonService.AllowNoParameter, EnableAllowNoParameter);
        //    }

        //    var list = await CommonService.SearchAll<TEntity>(param);

        //    var result = new List<TDto>();
        //    foreach (var item in list)
        //    {
        //        result.Add(item.MapTo<TDto>());
        //    }

        //    return new ListResponseContext<TDto> { Data = result };
        //}

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public virtual ListResponseContext<ColumnDataModel> GetColumnDataModel()
        {
            var columnDatas = GetColumnDataModel<TDto>();

            return new ListResponseContext<ColumnDataModel> { Data = columnDatas };
        }

        #endregion 数据获取

        /// <summary>
        /// 获取 Model对象 的属性转换成 列头
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <returns></returns>
        protected List<ColumnDataModel> GetColumnDataModel<TModel>() where TModel : class
        {
            return CommonService.GetColumnDataModel<TEntity, TModel>();
        }

        ///// <summary>
        ///// 获取 Model对象 的属性转换成 列头
        ///// </summary>
        ///// <typeparam name="TModel"></typeparam>
        ///// <returns></returns>
        //protected List<ColumnDataModel> GetColumnDataModel<TEntity, TModel>() where TModel : class where TEntity : Entity<TEntity>, new()
        //{
        //    return CommonService.GetColumnDataModel<TEntity, TModel>();
        //}

        ///// <summary>
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="ids"></param>
        ///// <returns></returns>
        //protected static async Task<IList<T>> FindAllByIds<T>(IEnumerable<long> ids) where T : Entity<T>, new()
        //{
        //    if (ids == null || ids.Count() == 0)
        //        return new List<T>();
        //    var param = new Dictionary<string, object> { { "id", ids } };
        //    var where = BuildWhereExpression<T>(param);
        //    return await Entity<T>.FindAllAsync(where);
        //}
    }
}